<!-- BaseTable.vue -->
<script setup>
import { ref, computed } from 'vue';

const props = defineProps({
  data: { type: Array, required: true },
  headers: { type: Array, required: true },
  loading: { type: Boolean, default: false },
  emptyText: { type: String, default: '暂无数据' }
});

const emit = defineEmits(['sort-change']);

// 排序状态
const sortBy = ref('');
const sortDirection = ref('asc');

// 排序方法
const handleSort = (value) => {
  if (sortBy.value === value) {
    sortDirection.value = sortDirection.value === 'asc' ? 'desc' : 'asc';
  } else {
    sortBy.value = value;
    sortDirection.value = 'asc';
  }

  // 通知父组件排序变化
  emit('sort-change', {
    sortBy: sortBy.value,
    direction: sortDirection.value
  });
};

// 计算显示的数据（考虑加载状态）
const displayData = computed(() => {
  if (props.loading) return [];
  return props.data;
});

// 检查是否为空
const isEmpty = computed(() => {
  return !props.loading && displayData.value.length === 0;
});
</script>

<template>
  <div class="base-table-container">
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-overlay">
      <div class="loading-spinner"></div>
      <span>加载中...</span>
    </div>

    <div class="table-wrapper">
      <table class="base-table">
        <thead>
        <tr>
          <th
              v-for="(header, index) in headers"
              :key="index"
              :class="{
                'sortable': header.sortable,
                'sorted': sortBy === header.value
              }"
              @click="header.sortable && handleSort(header.value)"
          >
            <div class="header-content">
              {{ header.text }}
              <span v-if="header.sortable && sortBy === header.value" class="sort-icon">
                  {{ sortDirection === 'asc' ? '↑' : '↓' }}
                </span>
            </div>
          </th>
        </tr>
        </thead>

        <tbody v-if="!isEmpty">
        <tr v-for="(row, rowIndex) in displayData" :key="rowIndex">
          <td v-for="(header, colIndex) in headers" :key="colIndex">
            <!-- 列插槽 -->
            <slot
                :name="header.slot || `cell-${header.value}`"
                :row="row"
                :value="row[header.value]"
                :index="rowIndex"
            >
              {{ row[header.value] }}
            </slot>
          </td>
        </tr>
        </tbody>
      </table>

      <!-- 空状态 -->
      <div v-if="isEmpty" class="empty-message">
        {{ emptyText }}
      </div>
    </div>
  </div>
</template>

<style scoped>
.base-table-container {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.table-wrapper {
  overflow-x: auto;
}

.base-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 600px;
}

.base-table th {
  background-color: #f8f9fa;
  text-align: left;
  padding: 12px 16px;
  font-weight: 600;
  color: #495057;
}

.base-table td {
  padding: 12px 16px;
  border-bottom: 1px solid #e9ecef;
}

.base-table tr:last-child td {
  border-bottom: none;
}

.sortable {
  cursor: pointer;
}

.sorted {
  background-color: #e9ecef;
}

.sort-icon {
  margin-left: 5px;
  font-weight: bold;
}

.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.8);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 10;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(0, 0, 0, 0.1);
  border-left-color: #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.empty-message {
  padding: 40px;
  text-align: center;
  color: #6c757d;
  font-size: 16px;
}
</style>